package presstrack
{
	import org.presstrack.service.CrudService;
	import flash.net.Responder;	
	import mx.utils.ObjectUtil;
	
	[RemoteClass(alias="presstrack.PaymentClass")]
	
	[Bindable]
	public class PaymentClass
	{
		public var iPaymentId:int;
		public var iPayerId:String;
		public var mPayerName:String;
		public var mCreationDate:String;
		public var mAccount:String;
		public var dSum:Number;
		public var iOrderId:int;
		public var iOrderLabel:int;		
		public var mNote:String;
		
		public function PaymentClass(ob:Object = null)
		{
			if (ob != null)
			{
				copyFrom(ob);
			}
			else
			{
				iPaymentId		= -1;
				iOrderId		= -1;
				mCreationDate	= CrudService.crud.getCurrentDate();
			}			
		}
		
		public function copyFrom(ob:Object):void
		{
			if(ob.hasOwnProperty("idpayment"))
			{
				iPaymentId = ob.idpayment;
			}
			
			if(ob.hasOwnProperty("order"))
			{
				iOrderId = ob.order;
			}
			
			if(ob.hasOwnProperty("customer_name"))
			{
				mPayerName = ob.customer_name;
			}
			
			if(ob.hasOwnProperty("customer_id"))
			{
				iPayerId = ob.customer_id;
			}
			
			if(ob.hasOwnProperty("creation"))
			{
				mCreationDate = ob.creation;
			}
			
			if(ob.hasOwnProperty("account"))
			{
				mAccount = ob.account;
			}
			
			if(ob.hasOwnProperty("sum"))
			{
				dSum = ob.sum;
			}
			
			if(ob.hasOwnProperty("order_label"))
			{
				iOrderLabel = ob.order_label;
			}
			
			if(ob.hasOwnProperty("note"))
			{
				mNote = ob.note;
			}
		}
		
		public function update(payment		:PaymentClass, 
							   resFunc		:Function		= null, 
							   faultFunc	:Function		= null):void 
		{
			CrudService.crud.srv.call("PressTrack.UpdatePayment", new Responder(onResult, onFault), 
					 iPaymentId, payment.iOrderId, 
					 payment.mCreationDate, payment.mAccount, 
					 payment.dSum, payment.mNote);	
			
			function onResult(res:String):void 
			{
				if (res == 'successful') 
				{
					iOrderId 		= payment.iOrderId;
					iOrderLabel		= payment.iOrderLabel;
					iPayerId		= payment.iPayerId;
					mPayerName		= payment.mPayerName;
					mCreationDate 	= payment.mCreationDate;
					mAccount 		= payment.mAccount;
					dSum 			= payment.dSum;
					mNote 			= payment.mNote;
										
					CrudService.crud.srv.call("PressTrack.GetOrder", new Responder(onResult2), iOrderId);
					
					function onResult2(st:Object):void 
					{
						var order:OrderClass 	= new OrderClass(st);
						for each(var ord:OrderClass in CrudService.arStoreOrder) 
						{
							if (ord.iOrderId == payment.iOrderId) 
							{
								var ind:int = CrudService.arStoreOrder.getItemIndex(ord);
								CrudService.arStoreOrder[ind] = order;
								CrudService.arStoreOrder.refresh();								
								break;
							}							
						}
						if (resFunc != null)
						{
							resFunc();	
						}											
					}
				}
				else
				{
					faultFunc(res);
				}
			}
			
			function onFault(e:Object) : void {
				faultFunc(ObjectUtil.toString(e));
			}		
		}
	}
}